package com.gzhryc.common.secruity;

import com.gzhryc.common.logging.Logger;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class ImageCodeTools {

    static Logger log = Logger.getLogger(ImageCodeTools.class);

    public static VerifyCode get(String path,int width,int height,int blockWidth,int blockHeight){
        BufferedImage img1 = readImg(path,width,height);
        if(img1 != null) {
            // 创建一个随机数生成器类
            Random random = new Random();
            Integer x = blockWidth + random.nextInt(img1.getWidth() - blockWidth);
            Integer y = blockHeight + random.nextInt(img1.getHeight() - blockHeight);

            //截取
            BufferedImage img2 = cropImage(img1, x, y, blockWidth, blockHeight);

            //将截取的地方填充白色
            BufferedImage buffImg = new BufferedImage(blockWidth, blockHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D gd = buffImg.createGraphics();
            // 将图像填充为白色
            gd.setColor(Color.LIGHT_GRAY);
            gd.fillRect(0, 0, blockWidth, blockHeight);

            Graphics2D gd1 = img1.createGraphics();
            gd1.drawImage(buffImg, x, y, blockWidth, blockHeight, null);
            gd1.dispose();

            VerifyCode verifyCode = new VerifyCode();
            verifyCode.setX(x);
            verifyCode.setY(y);
            verifyCode.setImg1(img1);
            verifyCode.setImg2(img2);
            return verifyCode;
        }
        return null;
    }

    public static BufferedImage readImg(String path,Integer width,Integer height){
        try {
            File imageFile = new File(path);
            BufferedImage image = ImageIO.read(imageFile);
            //缩放
            BufferedImage newImage = new BufferedImage(width, height, image.getType());
            Graphics graphics = newImage.getGraphics();
            graphics.drawImage(image, 0, 0, width, height, null);
            graphics.dispose();
            return newImage;
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null;
    }

    /**
     * 截取图片的一部分
     * @param image
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage cropImage(BufferedImage image, int x, int y, int width, int height) {
        try {
            BufferedImage croppedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = croppedImage.createGraphics();
            g2d.drawImage(image, 0, 0, width, height, x, y, x + width, y + height, null);
            g2d.dispose();
            return croppedImage;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static class VerifyCode {

        private Integer x;
        private Integer y;
        private BufferedImage img1;    //主图
        private BufferedImage img2;    //附图

        public Integer getX() {
            return x;
        }

        public void setX(Integer x) {
            this.x = x;
        }

        public Integer getY() {
            return y;
        }

        public void setY(Integer y) {
            this.y = y;
        }

        public BufferedImage getImg1() {
            return img1;
        }

        public void setImg1(BufferedImage img1) {
            this.img1 = img1;
        }

        public BufferedImage getImg2() {
            return img2;
        }

        public void setImg2(BufferedImage img2) {
            this.img2 = img2;
        }

        public byte[] getBytes1(){
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                if(ImageIO.write(img1, "jpg", out)){
                    return out.toByteArray();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        public byte[] getBytes2(){
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                if(ImageIO.write(img2, "jpg", out)){
                    return out.toByteArray();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
